package nahuel.games.carracer;

import java.util.Random;

import nahuel.games.carracer.Juego.Personaje;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;

public class Trafico extends Auto {

	public static final int NORTE = 1;
	public static final int SUR = 0;
	public static final int IZQUIERDA = 1;
	public static final int DERECHA = 0;
	public static final int HORIZONTAL = 1;
	public static final int VERTICAL = 0;
	
	private int velRelativaY = 0;
	private int sentido;
	private int direccion;

	public Trafico(GameView vista, int sentido, int direccion, int anchoD, int altoD){
		super(vista, Personaje.trafico, anchoD, altoD);
		this.sentido = sentido;
		this.direccion = direccion;
		inicTrafico();
		posInicial();
	}

	private void inicTrafico(){
		Matrix mMatrix = new Matrix();
		if(sentido == VERTICAL){
			this.velY = VEL_CRUCERO - (VEL_CRUCERO * 2 * direccion);	//SUR = CRUCERO, NORTE = -CRUCERO
			this.velX = 0;
			mMatrix.setRotate(180 - 180 * direccion);	//SUR = 180, NORTE = 0
		} else {
			this.velY = 0;
			this.velX = VEL_CRUCERO - (VEL_CRUCERO * 2 * direccion);	//DER = CRUCERO, IZQ = -CRUCERO
			mMatrix.setRotate(90 + 180 * direccion);	//DER = 90, IZQ = 270
		}
		this.bmp = Bitmap.createBitmap(this.bmp, 0, 0, width, height,
				mMatrix, false);
	}
	
	@Override
	public void posInicial() {
		if (sentido == VERTICAL) {
			x = (anchoD / 3) + direccion * (anchoD / 3);
			y = -(height * 2) + (direccion * (altoD + (height * 2)));
			Random rand = new Random();
			if (direccion == NORTE) {
				if (rand.nextInt(10) < 8) {
					y = -(height * 2);
				}
			}
		} else {
			x = -(width * 2) + (direccion * (anchoD + (width * 2)));
			y = altoD / 2;	//Se setea al dibujar el trafico sobre la esquina
		}
	}

	@Override
	public boolean chequeoLimites(){
		if (x < gameView.getWidth() * 8 / 45) {
			x = gameView.getWidth() * 8 / 45;
		}
		if (x > (gameView.getWidth() * 37 / 45) - width) {
			x = (gameView.getWidth() * 37 / 45) - width;
		}
		if (sentido == VERTICAL) {
			this.velY = VEL_CRUCERO - (VEL_CRUCERO * 2 * direccion);
			this.velX = 0;
		} else {
			this.velY = 0;
			this.velX = VEL_CRUCERO - (VEL_CRUCERO * 2 * direccion);
		}
		return ( ((y + (height * 3) < 0) && (sentido == NORTE))  || ((y > gameView.getHeight() + (height * 2)) && (sentido == SUR)) );
	}

	@Override
	public void choque(Auto otro) {
		this.velY = -otro.velY * 3/2;
		this.velX = otro.velX * 3/2;
		otro.velY = otro.velY * 1/3;
		otro.velX = -otro.velX * 3/2;
		otro.vida.bajarVida();
		update();
	}

	public void update() {
		y += gameView.escalarVelocidadY(velY + velRelativaY);
	}

	public void update(int velRelatY) {
		y += gameView.escalarVelocidadY(velY + velRelatY);
		x += gameView.escalarVelocidadX(velX);
	}

	public void draw(Canvas canvas, int posY) {
		int yTemp = (posY + (width / 3)) - (direccion * (width / 3 * 2 + width));
		canvas.drawBitmap(bmp, x, yTemp, null);
	}

	public void setVelocidadRelativa(int x, int y) {
		this.velRelativaY = y;
	}
	
	public int getSentido() {
		return sentido;
	}

	public int getDireccion() {
		return direccion;
	}

	public int getPosX() {
		return x;
	}

	public int getPosY() {
		return y;
	}

	public int setPosX(int x) {
		return this.x = x;
	}

	public int setPosY(int y) {
		return this.y = y;
	}
}

